﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Secured_EditProfile : System.Web.UI.Page
{
    SqlConnection conn;
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        conn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
        if (!Page.IsPostBack)
        {
            ViewState["PrevPage"] = Request.UrlReferrer;
            try
            {
                conn.Open();
                FillFVEditProfile();
            }
            catch (SqlException ex)
            {
                ltMsg.Text = ex.Message;
            }
        }
        fvEditProfile.FindControl("btnBack").Focus();
    }
    private void FillFVEditProfile()
    {
        cmd = new SqlCommand("Select UserID, UserName, FirstName, LastName, DisplayName, DOB, EmailAddr, SecretQuestion, SecretAnswer, IsApproved, LastLogin, ForcePasswordChangedDate from HAT_UserAccounts where UserName=@UName", conn);
        da = new SqlDataAdapter(cmd.CommandText, conn);
        da.SelectCommand.Parameters.Add("@UName", SqlDbType.VarChar).Value = Page.User.Identity.Name.ToString();
        ds = new DataSet();
        try
        {
            da.Fill(ds, "NewTable");
            da.Fill(ds, "Table2");
            fvEditProfile.DataSource = ds.Tables["NewTable"].DefaultView;
            fvAccountInfo.DataSource = ds.Tables["Table2"].DefaultView;
            fvEditProfile.DataBind();
            fvAccountInfo.DataBind();
        }
        catch (SqlException ex)
        {
            ltMsg.Text = ex.Message;
        }
    }
    protected void btnBack_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName.Equals("Back"))
        {
            Response.Redirect(ViewState["PrevPage"].ToString(), true);
        }
    }
    protected void btnSaveChanges_Command(object sender, CommandEventArgs e)
    {
        Type cstype = this.GetType();
        TextBox FName = (TextBox)fvEditProfile.FindControl("txtFirstName");
        TextBox LName = (TextBox)fvEditProfile.FindControl("txtLastName");
        TextBox DName = (TextBox)fvEditProfile.FindControl("txtDisplayName");
        TextBox DOB = (TextBox)fvEditProfile.FindControl("txtDOB");
        TextBox EMail = (TextBox)fvEditProfile.FindControl("txtEMail");
        TextBox SecQues = (TextBox)fvEditProfile.FindControl("txtSecQuestion");
        TextBox SecAns = (TextBox)fvEditProfile.FindControl("txtAnswer");
        if (e.CommandName.Equals("SaveChanges"))
        {
            cmd = new SqlCommand("update HAT_UserAccounts set FirstName=@Fname, LastName=@LName, DisplayName=@DName, DOB=@DOB, EmailAddr=@Email, SecretQuestion=@SecQues, SecretAnswer=@SecAns where UserName=@UName", conn);
            cmd.Parameters.Add("@FName", SqlDbType.VarChar).Value = FName.Text.Trim();
            cmd.Parameters.Add("@LName", SqlDbType.VarChar).Value = LName.Text.Trim();
            cmd.Parameters.Add("@DName", SqlDbType.VarChar).Value = DName.Text.Trim();
            cmd.Parameters.Add("@DOB", SqlDbType.DateTime).Value = Convert.ToDateTime(DOB.Text);
            cmd.Parameters.Add("@Email", SqlDbType.VarChar).Value = EMail.Text.Trim();
            cmd.Parameters.Add("@SecQues", SqlDbType.VarChar).Value = SecQues.Text.Trim();
            cmd.Parameters.Add("@SecAns", SqlDbType.VarChar).Value = SecAns.Text.Trim();
            cmd.Parameters.Add("@UName", SqlDbType.VarChar).Value = Page.User.Identity.Name;
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                Page.ClientScript.RegisterClientScriptBlock(cstype, "ChangesSaved", "alert('Changes saved Successfully!')", true);
                Response.Redirect(ViewState["PrevPage"].ToString(), true);
            }
            catch (SqlException ex)
            {
                ltMsg.Text = ex.Message;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
